//
//  VistorView.swift
//  TTWeibo
//
//  Created by 唐磊guang on 16/7/3.
//  Copyright © 2016年 唐磊guang. All rights reserved.
//

import UIKit

protocol VistorViewDelegate: NSObjectProtocol {
    func registerButtonWillClick()
    func loginButtonWillClick()
}

class VistorView: UIView {
    
    weak var delegate:VistorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpVistor(isHome: Bool , imageName: String , message: String)  {
        iconView.hidden = !isHome
        homeIconView.image = UIImage(named: imageName)
        messageLabel.text = message
        if isHome {
            startAnimation()
        }
    }
    
    //mark: -- 按钮点击事件
    func registerButtonClick(){
        delegate?.registerButtonWillClick()
    }
    func loginButtonClick() {
        delegate?.loginButtonWillClick()
    }
    
    //设置动画
    private func startAnimation(){
        //创建动画
        let  anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.removedOnCompletion = false
        //添加动画到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
   private func setUpUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
    //背景图标
    iconView.snp_makeConstraints { (make) in
        make.centerX.centerY.equalTo(self)
    }
    homeIconView.snp_makeConstraints { (make) in
        make.center.equalTo(self)
    }
    messageLabel.snp_makeConstraints { (make) in
        make.centerX.equalTo(self)
        make.top.equalTo(iconView.snp_bottom).offset(10)
        make.width.equalTo(230)
    }
    registerButton.snp_makeConstraints { (make) in
        make.left.equalTo(messageLabel.snp_left)
        make.width.equalTo(100)
        make.height.equalTo(35)
        make.top.equalTo(messageLabel.snp_bottom).offset(20)
    }
    
    loginButton.snp_makeConstraints { (make) in
        make.right.equalTo(messageLabel.snp_right)
        make.width.equalTo(100)
        make.height.equalTo(35)
        make.top.equalTo(messageLabel.snp_bottom).offset(20)
    }
    maskIconView.snp_makeConstraints { (make) in
        make.left.right.top.bottom.equalTo(self)
    }
    
    
    }
    //MARK: --懒加载
    lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    /// 遮罩视图
    // 注意系统有一个叫做maskView的属性, 属性名称不能叫做maskView
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
    }()
    
    /// 小房子
    private lazy var homeIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
    /// 消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VistorView.registerButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VistorView.loginButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
